package capitulo8;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MouseTest extends JFrame implements MouseListener, MouseMotionListener{
	// Declare some mouse event variables
	int clickX, clickY;
	int pressX, pressY;
	int releaseX, releaseY;
	int enterX, enterY;
	int exitX, exitY;
	int dragX, dragY;
	int moveX, moveY;
	int mouseButton;
	
	Random rand = new Random();
	int red, green, blue;
	
	public static void main(String[] args) {
		new MouseTest();
	}

	public MouseTest(){
		super("Mouse Test");
		setSize(500,400);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		g2d.setColor(Color.WHITE);
		g2d.fill(new Rectangle(0,0,500,400));
		g2d.setColor(Color.BLACK);
		g2d.drawString("Mouse clicked " + mouseButton + "at " + clickX + "," + clickY, 10,40);
		g2d.drawString("Mouse entered at " + enterX + "," + enterY, 10, 55);
		g2d.drawString("Mouse exited at " + exitX + "," + exitY, 10, 70);
		g2d.drawString("Mouse pressed " + mouseButton + " at " + pressX + ","+ pressY, 10,85);
		g2d.drawString("Mouse released " + mouseButton + " at " + releaseX + "," + releaseY, 10,100);
		g2d.drawString("Mouse dragged at " + dragX + "," + dragY, 10,115);
		g2d.drawString("Mouse Moved at " + moveX + "," + moveY, 10, 130);
		
		// If we do a left click then it we draw a point with a random color
		if (mouseButton == 1){
			red = rand.nextInt(256);
			green = rand.nextInt(256);
			blue = rand.nextInt(256);
			
			g2d.setColor(new Color(red,green,blue));
			g2d.fill(new Rectangle(clickX, clickY,1,1));
		}
	}
	
	// custom method called by mouse events to report button status
	private void checkButton(MouseEvent e){
		// check the mouse buttons
		switch(e.getButton()){
		case MouseEvent.BUTTON1:
			mouseButton = 1;
			break;
		case MouseEvent.BUTTON2:
			mouseButton = 2;
			break;			
		case MouseEvent.BUTTON3:
			mouseButton = 3;
			break;
		default: 
			mouseButton = 0;
		}
	}
	
	
	@Override
	public void mouseDragged(MouseEvent e) {
		dragX = e.getX();
		dragY = e.getY();
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		moveX = e.getX();
		moveY = e.getY();
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// Save the mouse position values
		clickX = e.getX();
		clickY = e.getY();
		
		// get an update on buttons
		checkButton(e);
		
		// Refresh the screen (call the paint event)
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		enterX = e.getX();
		enterY = e.getY();
		repaint();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		exitX = e.getX();
		exitY = e.getY();
		repaint();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		pressX = e.getX();
		pressY = e.getY();
		checkButton(e);
		repaint();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		releaseX = e.getX();
		releaseY = e.getY();
		checkButton(e);
		repaint();
	}
}
